<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

// make sure we are running within the framework
if(false === (isset($USER) && is_object($USER) && ($USER instanceof JFX_User)))
    include('system-start.php');

// get the output from previous scripts
$bodyContent = ob_get_contents();
ob_end_clean();


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After ob_get_contents()');


// add our direct output to the body
$VIEW->addBodyText($bodyContent);
$VIEW->prepare();


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After $VIEW->prepare()');


// set smarty defaults
$SMARTY->compile_check = true;
$SMARTY->debugging = true;
$SMARTY->force_compile = false;

// check if cache is to be used or not - cache is off by default
if(!$CONFIG->useSmartyCache || strlen($VIEW->cacheId) == 0){
    // don't use caching
    $SMARTY->caching = 0;
    $pageAsString = $SMARTY->fetch($VIEW->template);
}else{
    // use caching
    $SMARTY->caching = 2;
    $SMARTY->cache_lifetime = 86400;
    $pageAsString = $SMARTY->fetch($VIEW->template, $VIEW->cacheId);
};


$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After $SMARTY->fetch');



/*
if(!$USER->isMonitored()){
    $pageAsString2 = '';
};*/

// page load time
$pageLoadEndTime = getmicrotime();
$pageLoadTime = $pageLoadEndTime - $pageLoadStartTime;

$pageAsString = str_replace(':|pageLoadTime|:', substr($pageLoadTime, 0, 6).'secs', $pageAsString);




$JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Page is ready and compacted');



// our memory usage debugging
if($CONFIG->debug === true && $USER->isAdmin()){
    ob_start();
    $JFXMemoryDebugStartTime = $JFXMemDebug[0]['time'];
?>
    <table id="jfx-memory-debug">
        <tbody>
            <tr>
                <th>Microseconds</th>
                <th>Memory (kb)</th>
                <th>Description</th>
                <th>File</th>
                <th>Line</th>
            </tr>
<?php
    foreach($JFXMemDebug as $k=>$v){
        echo '<tr><td>'.round($v['time']-$JFXMemoryDebugStartTime, 4).'</td>
            <td>'.number_format(($v['bytes']/1024), 2, '.', ',').'</td>
            <td>'.$v['desc'].'</td>
            <td>'.$v['file'].'</td><td>'.$v['line'].'</td></tr>';
    }
?>
        </tbody>

    </table>
    <table id="jfx-db-debug">
        <tbody>
<?php
foreach($JFXDBDebug as $k=>$v){
   echo '<tr><th colspan="10">'.$v['query'].'</th></tr>';
   echo '<tr><th>ID</th><th>Select Type</th><th>Table</th><th>Type</th><th>Possible Keys</th>
<th>Key</th><th>Key Length</th><th>Ref</th><th>Rows</th><th>Extra</th></tr>';
   echo '<tr><td>'.$v['id'].'</td><td>'.$v['select_type'].'</td><td>'.$v['table'].'</td><td>'.$v['type'].'</td>
<td>'.$v['possible_keys'].'</td><td>'.$v['key'].'</td><td>'.$v['key_len'].'</td><td>'.$v['ref'].'</td>
<td>'.$v['rows'].'</td><td>'.$v['Extra'].'</td></tr>';
}
?>
        </tbody>

    </table>

<?php
    $debuggingOutput = ob_get_contents();
    ob_end_clean();



    $pageAsString = str_replace('<!-- JFX_DEBUGGING -->', $debuggingOutput, $pageAsString);


}

// compress the code
if($CONFIG->compressHtml === true){
    require_once($CONFIG->classesDir.'/ThirdParty/compactor.php');
    $compactor = new Compactor(array(
            'buffer_echo' => false,
            'compress_scripts' => false,
            'compress_css' => false
    ));
    //$pageAsString = $compactor->squeeze($pageAsString);
}

// tidy the code
if($CONFIG->tidyHtml === true && function_exists('tidy_parse_string')){

   // $pageAsString = tidy_parse_string($pageAsString);
}

// all done!
echo $pageAsString;

